// index.js
Page({
  data: {
    load_status: 0,
    data_list_loding_status: 1,
    data_bottom_line_status: false,
    data_list: [],
    banner_list: [],
    navigation: [],
    common_shop_notice: null,
    common_app_is_enable_search: 1,
    common_app_is_enable_answer: 1,
    common_app_is_header_nav_fixed: 0,
    common_app_is_online_service: 0,

    // 限时秒杀插件
    common_app_is_limitedtimediscount : 0,
    plugins_limitedtimediscount_data: null,
    plugins_limitedtimediscount_timer_title: '距离结束',
    plugins_limitedtimediscount_is_show_time: true,
  },
  onLoad: function(options) {
    // Do some initialize when page load.
  },
  onReady: function() {
    // Do something when page ready.
  },
  onShow: function() {
    this.init();
    // Do something when page show.
  },
  onHide: function() {
    // Do something when page hide.
  },
  onUnload: function() {
    // Do something when page close.
  },
  onPullDownRefresh: function() {
    // Do something when pull down.
  },
  onReachBottom: function() {
    // Do something when page reach bottom.
  },
  onShareAppMessage: function () {
   // return custom share data when user share.
  },
  onPageScroll: function() {
    // Do something when page scroll
  },
  // Event handler.
  viewTap: function() {
    console.log('view tap')
    this.setData({
      text: 'Set some data for updating view.'
    }, function() {
      // this is setData callback
    })
  },
  init(){
    var self = this;

    this.setData({
      data_list_loding_status : 1,
    });

    let task = tt.request({
      url: 'https://douyinshop.163ar.cn/api/index', // 目标服务器url
      method : "POST",
      data : {},
      success: (res) => {
        tt.stopPullDownRefresh();
        self.setData({load_status : 1});

        if (res.data.code == 0){
          var data = res.data.data;
          self.setData({
            data_bottom_line_status: true,
            banner_list: data.banner_list || [],
            navigation: data.navigation || [],
            data_list: data.data_list,
            common_shop_notice: data.common_shop_notice || null,
            common_app_is_enable_search: data.common_app_is_enable_search,
            common_app_is_enable_answer: data.common_app_is_enable_answer,
            common_app_is_header_nav_fixed: data.common_app_is_header_nav_fixed,
            data_list_loding_status: data.data_list.length == 0 ? 0 : 3,
            common_app_is_online_service: data.common_app_is_online_service || 0,
            common_app_is_limitedtimediscount: data.common_app_is_limitedtimediscount || 0,
            plugins_limitedtimediscount_data: data.plugins_limitedtimediscount_data || null,
          });
          // 限时秒杀倒计时
          if (this.data.common_app_is_limitedtimediscount == 1 && this.data.plugins_limitedtimediscount_data != null)
          {
            this.plugins_limitedtimediscount_countdown();
          }

        }else{
          self.setData({
            data_list_loding_status: 0,
            data_bottom_line_status: true,
          });

          app.showToast(res.data.msg);
        }
      },
        fail (res) {
          console.log(`request调用失败`);
          tt.stopPullDownRefresh();
          self.setData({
            data_list_loding_status : 2,
            data_bottom_line_status : true,
            load_status : 1,
          });
          app.showToast("服务器请求出错");
      }
    });

  },
  customData: {
    foo: 'bar'
  }
})